﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

namespace MikeProject002.MyClasses
{
    class Class_001_Static
    {
        public static System.Data.IDbConnection F_001_GetDatabaseConnection()
        {
            System.Configuration.ConnectionStringSettings settings = System.Configuration.ConfigurationManager.ConnectionStrings[1];
            MessageBox.Show(settings.ProviderName);
            System.Data.Common.DbProviderFactory factory = System.Data.Common.DbProviderFactories.GetFactory(settings.ProviderName);
            System.Data.Common.DbConnection conn = factory.CreateConnection();
            conn.ConnectionString = settings.ConnectionString;
            conn.Open();
            conn.Close();

            return conn;
        }

        public static void F_002_Using()
        {
            SqlConnection conn2;
            string source = System.Configuration.ConfigurationManager.ConnectionStrings[1].ConnectionString;
            using (SqlConnection conn = new SqlConnection(source))
            {
                conn.Open();
                string select = "select * from names";
                SqlCommand cmd = new SqlCommand(select, conn);


                conn2 = conn;

                object dr = cmd.ExecuteScalar();
                //conn.Close();
                /*
                while (dr.Read())
                {
                    MessageBox.Show(dr[3].ToString());


                }

                dr.Close();
                */


            }


        }

        public static void F_003_XP()
        {
            string source = System.Configuration.ConfigurationManager.ConnectionStrings[1].ConnectionString;
            SqlConnection conn = new SqlConnection(source);
            conn.Open();
            SqlCommand sqlXp = new SqlCommand("Proc_001_", conn);

            sqlXp.CommandType = System.Data.CommandType.StoredProcedure;
            sqlXp.Parameters.Add(new SqlParameter("@Change_ID", SqlDbType.Int, 0, "ID"));
            sqlXp.Parameters.Add(new SqlParameter("@Set_name", SqlDbType.NChar, 10, "Имя"));
            sqlXp.Parameters.Add(new SqlParameter("@Set_Fam", SqlDbType.NChar, 10, "Фамилия"));
            sqlXp.Parameters.Add(new SqlParameter("@Set_Out", SqlDbType.Int, 0, ParameterDirection.Output, false, 0,0, "ID", DataRowVersion.Default, null));

            sqlXp.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;

            sqlXp.Parameters[0].Value = 7;
            sqlXp.Parameters[1].Value = "Иимя";
            sqlXp.Parameters[2].Value = "Ффамилия";
            object i = sqlXp.ExecuteNonQuery();
            //MessageBox.Show(i.ToString());

            MessageBox.Show(sqlXp.Parameters[3].Value.ToString());

        }

        public static void F_004_DataSet_DataAdapter()
        {
            SqlConnection conn2;
            string source = System.Configuration.ConfigurationManager.ConnectionStrings[1].ConnectionString;
            using (SqlConnection conn = new SqlConnection(source))
            {
                conn.Open();
                string select = "select * from names";
                SqlDataAdapter da = new SqlDataAdapter(select, conn);
                DataSet ds = new DataSet();

                

                da.Fill(ds);

            
   
            }
        }

        public static void F_006_Anonim_Type()
        {
            Class_002_Disponsable Cl = new Class_002_Disponsable();

            var An = new { Cl.y, Cl.z };

            MessageBox.Show((Cl.y + Cl.z).ToString());

        }

        public static IEnumerable<object>  F_006_GetEnumerator()
        {
            yield return "Hello";
            yield return 5;
        } 





        
    }

    class Class_002_Disponsable : IDisposable
    {
        public int y = 5;
        public int z = 7;

        public Class_002_Disponsable(int i)
        {
            //MessageBox.Show("init");

        }

        public Class_002_Disponsable()
        {
            //MessageBox.Show("init-no");
        }

        public void Dispose()
        {
           
        }
    }




}
